/*
 * Ext GWT - Ext for GWT
 * Copyright(c) 2007-2009, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
package web.mail.server;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import web.app.client.model.Folder;
import web.mail.client.MailService;
import web.mail.client.model.MailItem;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class MailServiceImpl extends RemoteServiceServlet implements MailService {
	
	private static final long serialVersionUID = 650041489328665744L;

	private static final String PERSISTENCE_UNIT_NAME = "mail";
	private EntityManagerFactory factory;

	EntityManager em;
	
	@Override
	public Folder getMailFolders(String userId) {
		try{
			factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
			EntityManager em = factory.createEntityManager();

		Query q = em.createQuery("SELECT f FROM MailFolder f");
		q.getResultList();
		}catch (Exception e) {
			System.out.println( "ERROR" );
			System.out.println( e.getMessage() );
			e.printStackTrace();
		}
		return new Folder("ERROR");
	}
	
	@Override
	public List<MailItem> getMailItems(Folder folder) {
		// TODO Auto-generated method stub
		return null;
	}


/*
private Folder mail;


  
  private List<MailItem> inboxItems;
  private List<MailItem> sentItems;
  private List<MailItem> trashItems;

  public MailServiceImpl() {
    mail = new Folder();
    Folder inbox = new Folder("Inbox");
    Folder sent = new Folder("Sent Items");
    Folder trash = new Folder("Trash");

    List<MailItem> items = new ArrayList<MailItem>();// TODO: TestData.getMailItems();
    int count = items.size();
    
    inboxItems = new ArrayList<MailItem>();
    sentItems = new ArrayList<MailItem>();
    trashItems = new ArrayList<MailItem>();

    for (int i = 0; i < count; i++) {
      MailItem item = (MailItem) items.get(i);
      if (i < (count / 2)) {
        inboxItems.add(item);
      } else {
        sentItems.add(item);
      }
    }

    mail.add(inbox);
    mail.add(sent);
    mail.add(trash);
  }

  public Folder getMailFolders(String userId) {
    return mail;
  }

  public List<MailItem> getMailItems(Folder folder) {
    String name = folder.getName();
    if (name.equals("Inbox")) {
    	return inboxItems;
    } else if (name.equals("Sent Items")) {
      return sentItems;
    } else if (name.equals("Trash")) {
      return trashItems;
    }
    return new ArrayList<MailItem>();
  }
*/
}
